package com.qiang.test1;

import com.alibaba.fastjson.JSON;
import com.qiang.util.ListNode;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @package: com.qiang.test1
 * @title: LeetCode
 * @projectName: burning
 * @description: //TODO
 * @author zhuhongqiang3
 * @date:  2020-08-27 14:06
 * @version: V1.0
 * @retrun com.qiang.test1.burning
 */
public class LeetCode {

    /**
     * 功能描述: <br>
     * 〈链表反转〉---> 迭代
     * @param: [head]
     * @return: com.qiang.util.ListNode
     * @author: zhuhongqiang3
     * @date: 2020/9/8 10:20
     */
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode current = head;
        while (current != null) {
            ListNode nextTemp = current.next;
            current.next = prev;
            prev = current;
            current = nextTemp;
        }
        return prev;
    }


    /**
     * 功能描述: <br>
     * 〈数字反转  1234->4321〉
     * @param: [x]
     * @return: int
     * @author: zhuhongqiang3
     * @date: 2020/8/27 14:05
     */
    public static int reverse(int x) {
        long res = 0;
        while (x != 0 ){
            res = res * 10 + x % 10;
            x/=10;
        }
        return (int)res == res ? (int) res : 0;
    }

    /**
     * 功能描述: <br>
     * 〈从数组中 选取 2个数字之和 == target〉
     * @param: [nums, target]
     * @return: java.util.Map<java.lang.Integer, java.lang.Integer>
     * @author: zhuhongqiang3
     * @date: 2020/8/27 14:08
     */
    public static Map<Integer, Integer> twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    map.put(nums[i], nums[j]);
                }
            }
        }
        return map;
    }


    /**
     * 功能描述: <br>
     * 〈从数组中 选取 2个数字之和 == target〉
     * @param: [nums, target]
     * @return: java.util.Map<java.lang.Integer, java.lang.Integer>
     * @author: zhuhongqiang3
     * @date: 2020/8/27 14:08
     */
    public static int[][] twoSumArray(int[] nums, int target) {
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    int[] num = new int[]{i, nums[i],j, nums[j]};
                    list.add(num);
                }
            }
        }
        return list.toArray(new int[][]{new int[list.size()]});
    }

    private static Object[][] ListToArray(List<Map<String, Object>> list, int KeyLenght) {
        if (CollectionUtils.isEmpty(list)) {
            return new Object[0][];
        }
        int size = list.size();
        Object[][] array = new Object[size][KeyLenght];
        for (int i = 0; i < size; i++) {//循环遍历所有行
            array[i] = list.get(i).values().toArray();//每行的列数
        }
        return array;
    }

    public static void main(String[] args) {

        int a[] = {-10,10,20,30,40,50,60,70};

        int[][] ints = twoSumArray(a, 60);
        System.out.println(JSON.toJSONString(ints));
//
//        printListNode();

        /*System.out.println(Math.round(11.5));
        System.out.println(Math.round(-11.5));*/
    }

    private static void printListNode() {
        //组装链表
        ListNode start =new ListNode(0);
        ListNode prev = start;
        for (int i = 1; i < 10; i++) {
            ListNode current = new ListNode(i);
            prev.next = current;
            prev = current;
        }

        //开始正常输出

        ListNode initNode = start;
        while (start!=null){
            System.out.print(start.val+" > ");
            start = start.next;
        }
        System.out.println();

        LeetCode leetCode = new LeetCode();
        ListNode endNode = leetCode.reverseList(initNode);
        while (endNode!=null){

            System.out.print(endNode.val+" > ");
            endNode = endNode.next;
        }
    }

    /**
     * 功能描述: <br>
     * 〈判断一个整数是否是回文数。回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。
     *  输入: 121  输出: true
     *  输入: -121 输出: false 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
     * 〉
     * @param: [x]
     * @return: boolean
     * @author: zhuhongqiang3
     * @date: 2020/8/27 14:10
     */
    public static boolean isPalindrome(int x) {
        if(x < 0) return false;
        int result = reverse(x);
        return (x == result);
    }

    public static int romanToInt(String s) {
        int sum = 0;
        int left = getValue(s.charAt(0));
        for(int i = 1;i < s.length(); i ++) {
            int right = getValue(s.charAt(i));
            if(left < right) {
                sum -= left;
            } else {
                sum += left;
            }
            left = right;
        }
        sum += left;
        return sum;
    }

    private static int getValue(char ch) {
        switch(ch) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            default: return 0;
        }
    }

    public static Object[] mergeArray(int[] a, int [] b){
        List<int[]> res = new ArrayList<>();
        res.add(a);
        res.add(b);
        return res.toArray();
    }
}
